package ui.widget;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics2D;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

import ui.util.FontUtil;

public class ShowPromptDialog extends JDialog {

    private String str;
    private JLabel text;
    private Font font;

    public ShowPromptDialog(String s) {
        str = s;
		setUndecorated(true);
		setLocationRelativeTo(null);
		setAlwaysOnTop(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		font = new Font(null, Font.BOLD, 15);
		text = new JLabel(str, JLabel.CENTER);
		text.setFont(font);
		text.setForeground(Color.WHITE);
		Container c = getContentPane();
		c.add(text, BorderLayout.CENTER);
		c.setBackground(Color.black);
		pack();
		int width = (int)FontUtil.getFontWidth((Graphics2D)this.getGraphics(), font, str);
		setSize(width+40, 50);
        showIt();
    }
    public static void showMessage(String s){
        new ShowPromptDialog(s);
    }

    private void showIt() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                showAndDisapppear();
            }

        }).start();
    }

    private void showAndDisapppear() {
		setVisible(true);
        float t = 0.01f;
        setOpacity(0.0f);
        try {
            while (t < 1) {
                setOpacity(t);
                Thread.sleep(10);
                t += 0.04f;
            }
            Thread.sleep(1000);
            t = 1.0f;
            while (t > 0) {
                setOpacity(t);
				Thread.sleep(10);
				t -= 0.02f;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        dispose();
    }

}
